package com.xhwl.centiotdata.handler.eesd.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.centiotdata.handler.eesd.EesdHandler;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.service.cent.IEesdCentService;
import com.xhwl.common.constant.cent.IOTReceiveConstant;
import com.xhwl.common.dto.cent.alarm.iot.IotAlarmBody;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.dto.cent.device.iot.IotDeviceJson;
import com.xhwl.common.dto.cent.device.iot.NotifyData;
import com.xhwl.common.dto.cent.eesd.IotWaterMeter;
import com.xhwl.common.exception.DataException;
import com.xhwl.common.pojo.cent.energy.EnergyMeterRecord;
import com.xhwl.common.query.cent.device.DevicePageQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author Leon
 * @Description: TODO
 * @Date: 2021/12/28 15:57
 */
@Component
@Slf4j
public class EesdWaterMeterHandler extends EesdHandler {
    @Autowired
    IDeviceCentService deviceCentService;

    @Autowired
    IEesdCentService eesdCentService;

    @Override
    public void afterPropertiesSet() throws Exception {
        //todo  空实现
    }

    @Override
    public <T> void pushIotDAReceiveData(T t) {
        IotDeviceJson iotDeviceJson = JSONObject.parseObject(t.toString(), IotDeviceJson.class);
        NotifyData notifyData = iotDeviceJson.getNotify_data();
        JSONObject body = JSONObject.parseObject(notifyData.getBody().toString());
        List<IotAlarmBody> services = JSONArray.parseArray(body.get(IOTReceiveConstant.SERVICES).toString(), IotAlarmBody.class);

        for (int i = 0; i < services.size(); i++) {
            IotAlarmBody iotAlarmBody = services.get(i);
            if (iotAlarmBody.getService_id().equals(IOTReceiveConstant.WATERMETER)) {
                IotWaterMeter properties = null;
                try {
                    properties = JSONObject.parseObject(JSONObject.toJSONString(iotAlarmBody.getProperties()), IotWaterMeter.class);
                } catch (Exception e) {
                    log.info("====================>数据格式推送不正确！！！！");
                }
                DevicePageQuery devicePageQuery = new DevicePageQuery();
                devicePageQuery.setIotDeviceId(notifyData.getHeader().getDevice_id());
                Page<DeviceDTO> deviceDTOPage = deviceCentService.pageDevice(null, devicePageQuery);
                log.info("设备中台返回的数据=========================>{}", JSONObject.toJSONString(deviceDTOPage));

                if (CollectionUtils.isEmpty(deviceDTOPage.getRecords()))
                    throw new DataException("==================>设备中台返回的设备为空！<===================");

                DeviceDTO deviceDTO = deviceDTOPage.getRecords().get(0);

                if (Objects.nonNull(properties.getCumulant())) {
                    EnergyMeterRecord energyMeterRecord = new EnergyMeterRecord();
                    energyMeterRecord.setDeviceId(deviceDTO.getId());
                    energyMeterRecord.setProjectId(deviceDTO.getProjectId());
                    energyMeterRecord.setType(properties.getAttributeTypes());
                    energyMeterRecord.setReadNum(properties.getCumulant().doubleValue());
                    energyMeterRecord.setPushTime(Date.from(iotAlarmBody.getEvent_time().atZone(ZoneId.systemDefault()).toInstant()));
                    energyMeterRecord.setCreatedAt(new Date());
                    energyMeterRecord.setUpdatedAt(new Date());
                    energyMeterRecord.setDeviceType(10);
                    eesdCentService.insert(energyMeterRecord);
                }
            }
        }
    }
}
